namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class UserBuildBLL
    {
        public int Add(UserBuild model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into UserBuild(");
            builder.Append("BuildName,BuildCode");
            builder.Append(") values (");
            builder.Append("@BuildName,@BuildCode");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildName", model.BuildName), new SqlParameter("@BuildCode", model.BuildCode) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int BuildID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserBuild ");
            builder.Append(" where BuildID=@BuildID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuildID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string BuildIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from UserBuild ");
            builder.Append(" where ID in (" + BuildIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public string GetBuildCode(string parentCode)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) ");
            builder.Append(" FROM UserBuild ");
            builder.Append(" WHERE BuildCode LIKE '").Append(parentCode).Append("__'");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            int num = 0;
            if (table.Rows.Count > 0)
            {
                num = int.Parse(table.Rows[0][0].ToString());
            }
            int num2 = num + 1;
            return (parentCode + num2.ToString().PadLeft(2, '0'));
        }

        public List<UserBuild> GetCountList(int OrganID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Regist", OrganID) };
            DataTable table = DBHelper.RunProcedure("usp_UserBuild_Count", parameters).Tables[0];
            List<UserBuild> list = null;
            UserBuild item = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserBuild>();
                foreach (DataRow row in table.Rows)
                {
                    item = new UserBuild {
                        BuildID = int.Parse(row["BuildID"].ToString()),
                        BuildName = row["BuildName"].ToString(),
                        BuildCode = row["BuildCode"].ToString(),
                        Count_Allow = int.Parse(row["Count_Allow"].ToString()),
                        Count_Reality = int.Parse(row["Count_Reality"].ToString())
                    };
                    list.Add(item);
                }
            }
            return list;
        }

        public List<UserBuild> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM UserBuild ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            UserBuild item = null;
            List<UserBuild> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<UserBuild>();
                foreach (DataRow row in table.Rows)
                {
                    item = new UserBuild {
                        BuildID = int.Parse(row["BuildID"].ToString()),
                        BuildName = row["BuildName"].ToString(),
                        BuildCode = row["BuildCode"].ToString()
                    };
                    list.Add(item);
                }
            }
            return list;
        }

        public UserBuild GetModel(int BuildID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select BuildID, BuildName, BuildCode  ");
            builder.Append("  from UserBuild ");
            builder.Append(" where BuildID=@BuildID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildID", SqlDbType.Int, 4) };
            cmdParms[0].Value = BuildID;
            UserBuild build = new UserBuild();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                build.BuildID = int.Parse(row["BuildID"].ToString());
                build.BuildName = row["BuildName"].ToString();
                build.BuildCode = row["BuildCode"].ToString();
                return build;
            }
            return null;
        }

        public bool Update(UserBuild model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update UserBuild set ");
            builder.Append(" BuildName = @BuildName , ");
            builder.Append(" BuildCode = @BuildCode  ");
            builder.Append(" where BuildID=@BuildID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@BuildID", model.BuildID), new SqlParameter("@BuildName", model.BuildName), new SqlParameter("@BuildCode", model.BuildCode) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

